export default class Expand {

    constructor(node = null) {
        this.node = node;
        this.width = 16;
        this.height = 16;
        this.expanded = true;
     
        this.init();
        this.addEvent()
    }

    init() {
        this.g = new zrender.Group();
        this.vline = new zrender.Line({
            style: {
                stroke: '#ccc'
            },
            shape: {
                x1: 6,
                y1: 0,
                x2: 6,
                y2: 12
            },
            z:14
        });

        this.hline = new zrender.Line({
            style: {
                stroke: '#ccc'
            },
            shape: {
                x1: 0,
                y1: 6,
                x2: 12,
                y2: 6
            },
            z:14
        });

        this.circle = new zrender.Circle({
            style: {
                stroke: '#ccc',
                fill:'#fff'
            },
            shape: {
                cx: 6,
                cy: 6,
                r: 6
            },
            z:14
        });

        this.g.add(this.circle);
        this.g.add(this.vline);
        this.g.add(this.hline);
        this.vline.hide();
    }

    expand() {
        this.expanded = true;
        this.vline.hide();
    }

    collapse() {
        this.expanded = false;
        this.vline.show();
    }

    isExpand(){
        return this.expanded;
    }

    hide(){
        this.g.hide();
    }

    show(){
        this.g.show();
    }

    setPosition(x = 0, y = 0) {
        this.g.attr({
            position: [x, y]
        });
    }

    getPosition() {
        return {
            x: this.g.position.slice()[0],
            y: this.g.position.slice()[1]
        }
    }
    
    destroy() {
        this.g.removeAll();
        this.vline = null;
        this.hline = null;
        this.g = null;
    }

    getBox(){
        return this.g.getBoundingRect();
    }

    addEvent(){
        this.g.on('click',()=>{
            var eventName='expandNode';
            if(this.expanded){
               eventName='collapseNode';
            }
            this.g.__zr.trigger(eventName,{node:this.node})
        });
    }

}